package com.suyuan.controller.shop.admin;


import com.suyuan.entity.CustomInfo;
import com.suyuan.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/shop/admin/Customer")
public class Customer2Controller {

    @Resource
    private CustomerService customerService;


    /**
     * 查找所有用户信息
     * @param modelMap
     * @return
     */
    @RequestMapping("list")
    public String listCustomer(ModelMap modelMap){
        List<CustomInfo> customInfoList =  customerService.findAllCustomer();
        modelMap.addAttribute(customInfoList);
        return "customer_list";
    }


    /**
     * 跳转到编辑页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("toedit")
    public String toEdit(Integer id,ModelMap modelMap){
       CustomInfo customInfo =  customerService.findById(id);
       modelMap.addAttribute(customInfo);
        return "customer_edit";
    }


    /**
     * 更新用户信息
     * @param customeInfo
     * @return
     * @throws Exception
     */
    @RequestMapping("edit")
    public String edit(CustomInfo customeInfo) throws Exception {
        try{
            customerService.editCustomer(customeInfo);
        }catch (Exception e){
            throw new Exception("更新失败");
        }
        return "redirect:/shop/admin/Customer/list";
    }


    /**
     * 删除客户
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Object delete(Integer id){
        customerService.deleteCustomer(id);
        return "redirect:/shop/admin/Customer/list";
    }



}
